//
//  MessageLoginViewController.m
//  LoveDraw
//
//  Created by 零零壹 on 2017/7/11.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "MessageLoginViewController.h"

@interface MessageLoginViewController ()
@property (weak, nonatomic) IBOutlet UIView *PhoneV;
@property (weak, nonatomic) IBOutlet UIView *PassworldV;
@property (weak, nonatomic) IBOutlet UITextField *PhoneTF;
@property (weak, nonatomic) IBOutlet UITextField *MessageTF;
@property (weak, nonatomic) IBOutlet UIButton *MessageLoginBtn;
@property (weak, nonatomic) IBOutlet UIButton *SendMsg;

@property(nonatomic,assign)NSInteger index;

@end

@implementation MessageLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.index = 60;
    [self setUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userTextFieldDidChange) name:UITextFieldTextDidChangeNotification object:self.PhoneTF];
    // Do any additional setup after loading the view from its nib.
}
-(void)setUI
{
    self.PhoneV.layer.masksToBounds = YES;
    self.PhoneV.layer.cornerRadius = 5;
    self.PhoneV.layer.borderColor = UIColors(0xDFDFDF).CGColor;
    self.PhoneV.layer.borderWidth = 1;

    self.PassworldV.layer.masksToBounds = YES;
    self.PassworldV.layer.cornerRadius = 5;
    self.PassworldV.layer.borderColor = UIColors(0xDFDFDF).CGColor;
    self.PassworldV.layer.borderWidth = 1;

    self.MessageLoginBtn.layer.masksToBounds = YES;
    self.MessageLoginBtn.layer.cornerRadius = 5;

    self.SendMsg.layer.masksToBounds = YES;
    self.SendMsg.layer.cornerRadius = 5;

}

// 观察者模式
-(void)userTextFieldDidChange
{
    if (self.PhoneTF.text.length>0) {
        [self.MessageLoginBtn setTitleColor:UIColors(0xFFFFFF) forState:(UIControlStateNormal)];
        self.MessageLoginBtn.userInteractionEnabled = YES;
    }else
    {
        [self.MessageLoginBtn setTitleColor:PKCOLOR(200, 200, 200) forState:(UIControlStateNormal)];
        self.MessageLoginBtn.userInteractionEnabled = NO;
    }
}

/**
 发送验证码

 @param sender <#sender description#>
 */
- (IBAction)Send:(id)sender
{
    if (self.PhoneTF.text.length == 0) {
        [HUD showText:@"请先输入手机号码" AfterTime:1];
        return;
    }

    [RequestManager HudRequestWithUrl:@"/index.php?g=App&m=Login&a=appSendcode" parmDic:@{@"telephone":self.PhoneTF.text,@"UUID":[DeviceInfo getUUid],@"type":@"2"} requestType:RequestPOST view:self.view Success:^(id data)
    {
        NSTimer *timerAct = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(TimerHeadle:) userInfo:nil repeats:YES];
        [timerAct fire];
    } error:^(NSError *error)
    {
        
    }];
}


/**
 短信登录按钮

 @param sender <#sender description#>
 */
- (IBAction)MessageLogin:(id)sender
{
    if (self.MessageTF.text.length == 0) {
        [HUD showText:@"请输入短信验证码" AfterTime:1];
    }

    [RequestManager ProgressRequestWithUrl:@"/index.php?g=App&m=Login&a=loginCode" parmDic:@{@"username":self.PhoneTF.text,@"telephonecode":self.MessageTF.text,@"UUID":[DeviceInfo getUUid]} requestType:RequestPOST view:self.view Success:^(id data)
     {
         
         UserInfo *userinfo = [UserInfo mj_objectWithKeyValues:data];
         [UserInfoManage sharedInstance].userInfo = userinfo;
         UIWindow *window = [UIApplication sharedApplication].windows[0];
         UIViewController *root = [[NSClassFromString(@"RootViewViewController") alloc]init];
         window.rootViewController = root;
     } error:^(NSError *error) {
         
     }];

}


-(void)TimerHeadle:(NSTimer *)time;
{
    self.index--;
    self.SendMsg.enabled = NO;
    [self.SendMsg setTitle:[NSString stringWithFormat:@"%lds后发送",self.index] forState:(UIControlStateNormal)];
    self.SendMsg.backgroundColor = [UIColor grayColor];

    if (self.index == 0)
    {
        self.SendMsg.enabled = YES;
        self.SendMsg.backgroundColor = UIColors(0xFF8A2F);
        [self.SendMsg setTitle:@"获取验证码" forState:(UIControlStateNormal)];
        [time invalidate];
        self.index = 60;
    }
}

/**
 返回密码登录界面

 @param sender <#sender description#>
 */
- (IBAction)passworldAct:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
} 

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
